package com.sixth.oa.config;

import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;


import com.sixth.oa.service.impl.UserServiceImpl;

public class AuthRealm extends AuthorizingRealm {

    private String ClassName =this.getClass().getName();

//    @Autowired
//    @Lazy   //必须懒加载，否则Ehcache缓存注解及事务管理注解无效
//    private adminMapper userService;

    {
        super.setName(ClassName);
    }

    /**
     * 权限处理
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		return null;
//        String username = (String) principals.getPrimaryPrincipal();
//        // 从数据库或者缓存中获得角色数据
//        Set<String> roles = userService.getRolesByUserName(username);
//        Set<String> permissions = userService.getPermissionsByRoles(roles);
//        //上面的service层方法需要自己写
//        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        simpleAuthorizationInfo.setStringPermissions(permissions);
//        simpleAuthorizationInfo.setRoles(roles);
//
//        return simpleAuthorizationInfo;
    }

    /**
     * 认证处理
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		return null;
//        // 1.从主体传过来的认证信息中，获得用户名
//        String username = (String) token.getPrincipal();
//
//        // 2.通过用户名到数据库中获取凭证
//        String password = userService.getPasswordByUsername(username);
//        if(password == null) {
//            return null;
//        }
//        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, password, ClassName);
//        return simpleAuthenticationInfo;
    }

}
